import 'dart:io';

import 'package:flutter/material.dart';
import 'package:image/image.dart' as img;

import 'package:flutter/services.dart';
import 'package:heic_converter/heic_converter.dart';
import 'package:image_picker/image_picker.dart';
import 'package:wechat_assets_picker/wechat_assets_picker.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final _heicConverterPlugin = HeicConverter();
  final ImagePicker picker = ImagePicker();

  List<Uint8List> totalImages = [];

  @override
  void initState() {
    super.initState();
  }

  Future<void> chooseImage() async {
    final List<AssetEntity>? images = await AssetPicker.pickAssets(context);
    if (images != null) {
      for (var image in images) {
        File? file = await image.originFile;
        if (file != null) {
          final convertPath =
              await _heicConverterPlugin.convertToPng(file.path);
          if (convertPath != null) {
            img.Image? image = await img.decodeImageFile(convertPath);
            if (image != null) {
              img.Image result = img.copyResizeCropSquare(image, size: 1000);
              Uint8List imageData = img.encodeJpg(result, quality: 80);
              print("data: $imageData");
            }
          }
        }
      }
      setState(() {});
    }
  }

  Future<void> uploadImage() async {
    final List<AssetEntity>? images = await AssetPicker.pickAssets(
      context,
      pickerConfig:
          const AssetPickerConfig(maxAssets: 9, requestType: RequestType.image),
    );
    if (images != null) {
      for (var image in images) {
        File? file = await image.originFile;
        if (file != null) {
          final convertPath =
              await _heicConverterPlugin.convertToJpg(file.path);
          if (convertPath != null) {
            img.Image? image = await img.decodeImageFile(convertPath);
            if (image != null) {
              img.Image result = img.copyResizeCropSquare(image, size: 1000);
              Uint8List imageData = img.encodeJpg(result, quality: 80);
              totalImages.add(imageData);
            }
          }
        }
      }
    }

    // final List<XFile> images = await picker.pickMultiImage();
    // for (var image in images) {
    //   // final convertPath = await _heicConverterPlugin.convertToPng(image.path);
    //   final convertPath = await _heicConverterPlugin.convertToJpg(image.path);
    //   if (convertPath != null) {
    //     img.Image? image = await img.decodeImageFile(convertPath);
    //     if (image != null) {
    //       img.Image result = img.copyResizeCropSquare(image, size: 1000);
    //       Uint8List imageData = img.encodeJpg(result, quality: 80);
    //       totalImages.add(imageData);
    //     }
    //   }
    // }
    // setState(() {});

    // final XFile? image = await picker.pickImage(source: ImageSource.gallery);
    // if (image != null) {
    //   final convertPath = await _heicConverterPlugin.convertToPng(image.path);
    //   if (convertPath != null) {
    //     img.Image? image = await img.decodeImageFile(convertPath);
    //     if (image != null) {
    //       img.Image result = img.copyResizeCropSquare(image, size: 1000);
    //       Uint8List imageData = img.encodeJpg(result, quality: 80);
    //       print("data: $imageData");
    //     }
    //   }
    // }
  }

  Future<void> checkRoute() async {
    final res = await _heicConverterPlugin.isBluetoothAudioOutput();
    print("当前音频通道是否在蓝牙：${res == true ? '是' : '否'}");
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
          actions: [
            IconButton(
              // onPressed: uploadImage,
              onPressed: checkRoute,
              icon: const Icon(
                Icons.upload_file_rounded,
                color: Colors.black,
                size: 24,
              ),
            ),
          ],
        ),
        body: GridView.builder(
          shrinkWrap: true,
          padding: const EdgeInsets.all(10),
          gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 2,
            mainAxisSpacing: 10,
            crossAxisSpacing: 10,
            childAspectRatio: 1.0,
          ),
          itemBuilder: (c, i) => Image.memory(totalImages[i]),
          itemCount: totalImages.length,
        ),
      ),
    );
  }
}
